package org.jeecg.modules.controller.articleSet;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.articleSet.entity.ArticleSet;
import org.jeecg.common.modules.articleSet.service.IArticleSetService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: 文章设置
* @Author: jeecg-boot
* @Date:   2021-10-21
* @Version: V1.0
*/
@Api(tags="文章设置")
@RestController
@RequestMapping("api/v1/articleSet")
@Slf4j
public class ArticleSetController extends JeecgController<ArticleSet, IArticleSetService> {
   @Autowired
   private IArticleSetService articleSetService;

   /**
    * 分页列表查询
    *
    * @param articleSet
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "文章设置-分页列表查询")
   @ApiOperation(value="文章设置-分页列表查询", notes="文章设置-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(ArticleSet articleSet,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<ArticleSet> queryWrapper = QueryGenerator.initQueryWrapper(articleSet, req.getParameterMap());
       queryWrapper.select("id","article_title","head_img","reading_total");
       queryWrapper.orderByDesc("create_time");
       Page<ArticleSet> page = new Page<ArticleSet>(pageNo, pageSize);
       IPage<ArticleSet> pageList = articleSetService.page(page, queryWrapper);
       return Result.ok(pageList);
   }

   private void addRead(String id) {
       ArticleSet articleSet = articleSetService.getById(id);
       Integer readingTotal = articleSet.getReadingTotal();
       if(readingTotal.equals(null)){
           articleSet.setReadingTotal(0);
       }else {
           articleSet.setReadingTotal(articleSet.getReadingTotal()+1);
       }
   }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文章设置-通过id查询")
    @ApiOperation(value="文章设置-通过id查询", notes="文章设置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        ArticleSet articleSet = articleSetService.getById(id);
        if(articleSet==null) {
            return Result.error("未找到对应数据");
        }
        addRead(id);
        return Result.ok(articleSet);
    }
}
